package com.wenyu.wenyuyuedu.index;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.homepager.MemberManageBean;
import com.wenyu.wenyuyuedu.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/14.
 * 成员管理的adapter
 */

public class MenberIndexAdapter extends BaseAdapter implements SectionIndexer {

    private List<User> mList = new ArrayList<>();
    private Context context;
    private MyViewHolder holder;
    private itemOnClickListener itemOnClickListener;
    private final String TYPE1 = "title";
    private final String TYPE2 = "name";
    private String uid;
    private int crruntPosition;

    public void setItemOnClickListener(MenberIndexAdapter.itemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public MenberIndexAdapter(Context context, String uid) {
        this.context = context;
        this.uid = uid;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.menber_index_item, parent, false);
            holder = new MyViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_catalog);
            holder.btn_delete = (Button) convertView.findViewById(R.id.btn_delete);
            holder.membermange_headview = (CircleImageView) convertView.findViewById(R.id.membermange_headview);
            holder.tv_qunzhu = (TextView) convertView.findViewById(R.id.tv_qunzhu);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(position, v);
                }
            }
        });

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(position, v);
                }
            }
        });

        int section = getSectionForPosition(position);
        if (position == getPositionForSection(section)) {
            holder.tvTitle.setVisibility(View.VISIBLE);
            holder.tvTitle.setText(mList.get(position).getFirstLetter());
        } else {
            holder.tvTitle.setVisibility(View.GONE);
        }
        holder.tv.setText(mList.get(position).getName());
        Glide.with(context)
                .load(mList.get(position).getPath())
                .error(R.drawable.login_logo)
                .into(holder.membermange_headview);
        String uid = mList.get(position).getUid();
        if (this.uid.equals(uid)) {
            crruntPosition = position;
            holder.tv_qunzhu.setVisibility(View.VISIBLE);
        } else {
            holder.tv_qunzhu.setVisibility(View.GONE);
        }

        return convertView;
    }


    public void addAll(List<User> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public List<User> getmList() {
        return mList;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getCount(); i++) {
            char firstChar = mList.get(i).getFirstLetter().charAt(0);
            if (sectionIndex == firstChar) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (mList != null) {
            return mList.get(position).getFirstLetter().charAt(0);
        }
        return -1;
    }

    class MyViewHolder {
        public TextView tv, tvTitle, tv_qunzhu;
        public Button btn_delete;
        public CircleImageView membermange_headview;
    }

    public interface itemOnClickListener {
        void onClick(int position, View v);
    }

    private List<MemberManageBean.InfoBean> info = new ArrayList<>();

    public void setListAll(List<MemberManageBean.InfoBean> info) {
        if (info != null) {
            this.info = info;
        }
    }
}
